<template>
  <div>
    <el-table
      :data="goodsList"
      style="width: 100%; margin-bottom: 20px"
      row-key="id"
      border
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column prop="id" label="商品编号"> </el-table-column>
      <el-table-column prop="goodsname" label="商品名称"> </el-table-column>
      <el-table-column prop="price" label="价格"> </el-table-column>
      <el-table-column prop="market_price" label="市场价格"> </el-table-column>
      <el-table-column label="图片">
        <template slot-scope="scope">
          <div>
            <img :src="$pre + scope.row.img" alt="">
          </div>
        </template>
      </el-table-column>
      <el-table-column label="是否新品">
        <template slot-scope="scope">
          <div>
            <el-button type="primary" v-if="scope.row.ishot == 1">是</el-button>
            <el-button type="success" v-else>否</el-button>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="是否热卖">
        <template slot-scope="scope">
          <div>
            <el-button type="primary" v-if="scope.row.isnew == 1">是</el-button>
            <el-button type="success" v-else>否</el-button>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="状态">
        <template slot-scope="scope">
          <div>
            <el-button type="primary" v-if="scope.row.status == 1">启 用</el-button>
            <el-button type="success" v-else>禁 用</el-button>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="180">
        <template slot-scope="scope">
          <div>
            <el-button type="info" @click="edit(scope.row.id)">编 辑</el-button>
            <!-- <el-button type="primary" @click="del(scope.row.id)">删 除</el-button> -->
            <v-del @del="del(scope.row.id)"></v-del>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <!-- 
      background    是否为分页按钮添加背景色
      layout        组件布局
      total         数据的总总条数
      page-size     每页展示的条数
     -->
    <el-pagination background layout="prev, pager, next" :total="goodsCount" :page-size="goodsSize" @current-change="changePage">
    </el-pagination>
  </div>
</template>
<script>
import { mapGetters, mapActions } from "vuex";
import { goodsDel } from "../../../request/api";
import { successAlert, warningAlert } from "../../../utils/alert";
export default {
  props: [],
  components: {},
  data() {
    return {};
  },
  computed: {
    ...mapGetters({
      goodsList: "goods/list",
      goodsCount: "goods/count",
      goodsSize: "goods/size"
    }),
  },
  methods: {
    ...mapActions({
      asyncGoodsList: "goods/asyncList",
      asyncGoodsCount: "goods/asyncCount",
      asyncGoodsPage: "goods/asyncPage"
    }),
    // 点击删除
    del(id) {
      goodsDel({ id: id }).then((res) => {
        if (res.data.code == 200) {
          // 成功的提示窗
          successAlert(res.data.msg);
          // 刷新列表数据
          this.asyncGoodsList();
          // 重新请求列表总数
          this.asyncGoodsCount()
        } else {
          // 失败的提示窗
          warningAlert(res.data.msg);
        }
      });
    },
    // 点击编辑
    edit(id) {
      // 通知父组件，我点了编辑
      this.$emit("edit", id);
    },
    // 当分页中的当前页码发生改变
    changePage(num){
      this.asyncGoodsPage(num)
    }
  },
  mounted() {
    // 页面一进来就要将当前页码数变成1。状态层会做两件事，1、修改当前页码数。2、用page为1的页码数请求第一页的数据
    this.asyncGoodsPage(1)
    // 页面一进来请求商品总数
    this.asyncGoodsCount()
  },
};
</script>
<style scoped>
img{
  width: 100px;
  height: 100px;
}
</style>